/* Source file https://github.com/MrOtherGuy/firefox-csshacks/tree/master/chrome/multi-row_tabs_below_content.css made available under Mozilla Public License v. 2.0
See the above repository for updates as well as full license text. */

/* Makes tabs to appear on multiple lines below the web content
 * Tab reordering will not work and can't be made to work
 * Make menubar enabled for getting window controls in correct place.
 * NOTE! menubar will be hidden even if enabled, if you want to be able to show menubar items occasionally,
 * then add single flexible space on the left side of menubar items - doing this creates "button" which when hovered
 * shows menubar items at the top left corner. See the custom pref below to make menubar static instead.
 * It's recommended to move tabs new-tab-button outside tabs toolbar */

/* SETTINGS (you need to create and set these prefs to true in about:config)
 * userchrome.multirowtabs.full-width-tabs.enabled - make tabs grow horizontally to fill all available space
 * userchrome.multirowtabs.static-menubar.enabled  - always show menubar */

@-moz-document url(chrome://browser/content/browser.xhtml){
  
  :root{
    --multirow-toolbar-height: 40px;
    --multirow-top-padding: 0px;
    --multirow-n-rows: 3; /* change maximum number of rows */
    --multirow-tab-min-width: 100px;
    --multirow-tab-dynamic-width: 1; /* Change to 0 for fixed-width tabs using the above width. */
    --uc-window-drag-space-width: 30px;
    --uc-window-control-width: 138px;
  }
  :root[customizing]{
    --multirow-top-padding: 32px;
  }
  :root[inFullscreen]{
    --uc-window-drag-space-width: 0px;
  }
  @media (-moz-gtk-csd-available){
    :root{
      --uc-window-control-width:  94px;
    }
  }
  
  :root[uidensity="compact"]{ --multirow-toolbar-height: 34px }
  :root{ border-top-width: 0px !important }

  #navigator-toolbox{
    order: 2;
    border-bottom: none !important;
  }
  /* Re-order window and tab notification boxes */
  #navigator-toolbox > div{ display: contents }
  .global-notificationbox,
  #tab-notification-deck{
    order: -1;
  }

  #titlebar{
    -moz-appearance: none !important; /* Try setting to "-moz-window-titlebar" if you face issues */
    position: relative;
    z-index: 1
  } 

  #nav-bar{
    position: fixed !important;
    border-left: var(--uc-window-drag-space-width) solid transparent !important;;
    top: var(--multirow-top-padding);
    border-top: 0 !important;
    background: inherit;
    background-origin: border-box;
    width: 100vw;
  }
  :root[inFullscreen] #nav-bar,
  :root[tabsintitlebar] #nav-bar{
    border-right: var(--uc-window-control-width) solid transparent !important;
  }
  :root[sizemode="fullscreen"] #nav-bar:not(:hover){
    opacity: 0;
  }

  #TabsToolbar > .titlebar-buttonbox-container{
    display: none !important;
  }  
  /* Fix incorrect vertical alignment with megabar */
  #urlbar[breakout]{ --urlbar-container-height: var(--multirow-toolbar-height) !important; }

  /* Fix customization view */
  #customization-panelWrapper > .panel-arrowbox > .panel-arrow{ margin-inline-end: initial !important; }
  
  /* Fullscreen mode support */
  #customization-container,
  :root:not([inFullscreen]) #content-deck,
  :root:not([chromehidden~="toolbar"]):not([inFullscreen]) #browser {
    margin-top: calc(var(--multirow-toolbar-height) + var(--multirow-top-padding) + var(--multirow-menubar-height,0px));
  }
  :root[inFullscreen] #toolbar-menubar,
  :root[inFullscreen] #nav-bar{
    top: calc(1px - (var(--multirow-toolbar-height) + var(--multirow-top-padding)));
  }
  :root[inFullscreen] #navigator-toolbox:hover > #nav-bar,
  :root[inFullscreen] #navigator-toolbox:hover > #titlebar > #toolbar-menubar{
    top: 0px;
  }
  #navigator-toolbox[inFullscreen]{
    margin-top: 0 !important;
  }
  #navigator-toolbox[inFullscreen][style*="margin-top"]:not(:hover){
    max-height: 0px;
  }
  #fullscr-toggler{ bottom: 0; top: unset !important; }
  :root[inFullscreen] #navigator-toolbox:hover #toolbar-menubar{
    pointer-events: none !important;
  }
  
  /* Restyle nav-bar and menubar */
  #nav-bar-customization-target{ flex-grow: 1 }

  #nav-bar-overflow-button,
  #PanelUI-menu-button{ height: var(--multirow-toolbar-height); }

  #toolbar-menubar{
    height: var(--multirow-toolbar-height);
    position: fixed;
    display: flex;
    visibility: hidden;
    top: var(--multirow-top-padding);
    padding-top: 0 !important;
    width: 100vw;
    color: var(--lwt-text-color);
  }
  
  :root[customizing] #toolbar-menubar{
    visibility: visible;
    top: 0;
  }
  #toolbar-menubar > toolbarspring{
    visibility: visible;
  }
  #menubar-items + spacer{ flex-grow: 1 }
  
  #toolbar-menubar > #menubar-items{
    height: 100%;
  }
  #main-menubar{ height: var(--multirow-toolbar-height) }

  #toolbar-menubar > toolbarspring:hover + #menubar-items,
  #menubar-items:hover{
    visibility: visible;
    background-image: linear-gradient( to left,transparent,-moz-dialog 35px);
  }
  #toolbar-menubar:not(:hover,[customizing]) *{-moz-window-dragging: drag !important;}
  #toolbar-menubar > .titlebar-buttonbox-container{
    order:1000;
    visibility: visible;
  }
  #menubar-items + spacer{ order: 1000 }
  :root[sizemode="fullscreen"] #toolbar-menubar spacer{ visibility: visible }
  #toolbar-menubar > .titlebar-buttonbox-container > .titlebar-buttonbox{
    height: 100%;
    pointer-events: auto !important;
  }

  #main-menubar:last-child{ padding-inline-end: 40px }
  #toolbar-menubar > toolbarspring {
    background: url("chrome://global/skin/icons/settings.svg") no-repeat 10px;
    height: 100%;
    display: flex;
    fill: currentColor;
    -moz-context-properties: fill;
    min-width: 28px !important;
    max-width: 28px !important;
  }

  :root[tabsintitlebar] #PanelUI-button{ margin-right: var(--uc-window-drag-space-width); }

  #tabbrowser-tabs{
    min-height: unset !important;
    padding-inline-start: 0px !important
  }

  #scrollbutton-up~spacer,
  #scrollbutton-up,
  #scrollbutton-down{ display: var(--scrollbutton-display-model,initial) }

  scrollbox[part][orient="horizontal"] > slot,
  scrollbox[part][orient="horizontal"]{
    display: flex;
    flex: 1;
    flex-wrap: wrap;
    overflow-y: auto;
    max-height: var(--uc-scrollbox-max-height,1px);
    scrollbar-color: currentColor transparent;
    scrollbar-width: thin;
    scrollbar-gutter: stable;
    scroll-snap-type: y mandatory;
  }
  .scrollbox-clip > scrollbox[part][orient="horizontal"] > slot{ display: contents }
  scrollbox[part][orient="horizontal"] > slot{
    overflow-x: hidden;
  }
  .scrollbox-clip[orient="horizontal"],
  #tabbrowser-arrowscrollbox{
    overflow: clip;
    display: inline;
    --scrollbutton-display-model: none;
    --uc-scrollbox-max-height: calc((var(--tab-min-height) + 2 * var(--tab-block-margin,0px)) * var(--multirow-n-rows));
    -moz-window-dragging: no-drag;
  }
  
  :root[inFullscreen] .scrollbox-clip[orient="horizontal"]:not(:hover),
  :root[inFullscreen] #tabbrowser-arrowscrollbox:not(:hover){ --uc-scrollbox-max-height: 1px; min-height: 1px !important; }

  .tabbrowser-tab{
    height: calc((var(--tab-min-height) + 2 * var(--tab-block-margin,0px))) !important;
    scroll-snap-align: start;
  }

  .tabbrowser-tab[pinned]{
    position: static !important;
    margin-inline-start: 0px !important;
  }

  .tabbrowser-tab[fadein]:not([pinned]){
    min-width: var(--multirow-tab-min-width) !important;
    flex-grow: var(--multirow-tab-dynamic-width) !important;
  }

  .tabbrowser-tab > stack{ width: 100%; height: 100%; }

  /* This should fix potential flash that could occur when bottom border is hovered to show tabs in fullscreen mode */
  #tabbrowser-tabpanels{ background-color: var(--lwt-accent-color) !important; }

  /* remove bottom margin so it doesn't throw off row height computation */
  #tabs-newtab-button{ margin-bottom: 0 !important; }
  
  /* Hide some things that are unnecessary in multi-row below content mode */

  #alltabs-button,
  :root:not([customizing]) #TabsToolbar #new-tab-button,
  #tabbrowser-arrowscrollbox > spacer,
  .tabbrowser-tab::after{ display: none !important } /* Also disables tab separators since they mysteriously break tab-row scrolling */
  :root[BookmarksToolbarOverlapsBrowser] #PersonalToolbar{
    position: absolute;
    top: calc(var(--multirow-toolbar-height) + var(--multirow-top-padding));
    width: 100vw;
  }
  :root[BookmarksToolbarOverlapsBrowser] #navigator-toolbox{
    margin-bottom: 0 !important;
    z-index: auto !important;
  }
  @media (-moz-bool-pref: "userchrome.multirowtabs.static-menubar.enabled"),
         -moz-pref( "userchrome.multirowtabs.static-menubar.enabled"){
    :root:not([sizemode="fullscreen"]){
      --multirow-menubar-height: 28px;
      --uc-window-drag-space-width: 0px;
      --uc-window-control-width: 0px;
    }
    #toolbar-menubar{
      visibility: visible;
      background-image: var(--lwt-additional-images);
      background-repeat: var(--lwt-background-tiling);
      background-position: var(--lwt-background-alignment);
    }
    :root[sizemode="fullscreen"] #toolbar-menubar{
      visibility: hidden;
    }
    :root[sizemode="fullscreen"] #menubar-items{
      visibility: collapse !important;
    }
    :root[lwtheme-image] #toolbar-menubar {
      background-image: var(--lwt-header-image), var(--lwt-additional-images);
      background-repeat: no-repeat, var(--lwt-background-tiling);
      background-position: right top, var(--lwt-background-alignment);
    }
    #nav-bar{
      background-position-y: calc(0px - var(--multirow-menubar-height));
    }
    #main-menubar,
    #toolbar-menubar{ height: var(--multirow-menubar-height); }
    #toolbar-menubar > #menubar-items{ visibility: visible; background-image: none !important; }
    .titlebar-buttonbox-container{ background: none !important }
    :root:not([inFullscreen]) #nav-bar{
      top: calc(var(--multirow-menubar-height) + var(--multirow-top-padding));
    }
    :root[BookmarksToolbarOverlapsBrowser] #PersonalToolbar{
      top: calc(var(--multirow-toolbar-height) + var(--multirow-top-padding) + var(--multirow-menubar-height));
    }
  }
  @media (-moz-bool-pref: "userchrome.multi-row-tabs.full-width-tabs.enabled"),
         -moz-pref("userchrome.multi-row-tabs.full-width-tabs.enabled"){
    .tabbrowser-tab[fadein]:not([pinned]){ max-width: 100vw !important; }
  }
}

